package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.accounting.parameter.AccountEntryBean;
import com.ymatou.iapi.accounting.parameter.AccountingBean;
import com.ymatou.iapi.accounting.parameter.AccountingItemBean;
import com.ymatou.iapi.accounting.parameter.CreateAccountBean;
import com.ymatou.iapi.accounting.parameter.GetAccountBalanceBean;
import com.ymatou.iapi.accounting.parameter.GetAccountByUserIdBean;
import com.ymatou.iapi.accounting.parameter.GetAccountEntryListBean;
import com.ymatou.iapi.accounting.parameter.SetAccountLockStatusBean;
import com.ymatou.iapi.accounting.service.AccountingCall;
import com.ymatou.iapi.accounting.service.CreateAccountCall;
import com.ymatou.iapi.accounting.service.GetAccountBalanceCall;
import com.ymatou.iapi.accounting.service.GetAccountByUserIdCall;
import com.ymatou.iapi.accounting.service.GetAccountEntryListCall;
import com.ymatou.iapi.accounting.service.SetAccountLockStatusCall;
import com.ymt.core.tool.Logger;
import com.ymttest.common.enums.AccountOperateTypeEnum;
import com.ymttest.database.model.Account;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :AccountCallServiceV2.java
 * @Author : zhouyi
 * @Date : 2018年1月2日
 * @Copyright : 洋码头
 * 用于mysql改造
 ************************************************************************************/
public class AccountCallServiceV2 {

	/**
	 * 获取AccountId，如果没创建一个
	 * 
	 * @param uid UserId 默认查询 AccountType 为1的账户信息
	 * @return accountId
	 * @throws Exception
	 */
	public static String getAccountId(int uid) throws Exception {
		String aid = null;
		try {
			Logger.comment("获取Uid" + uid + "对应的AccountId");
			// 查询Uid对应的Accountid
			GetAccountByUserIdBean getaccountbyuseridBean = new GetAccountByUserIdBean();
			GetAccountByUserIdCall getaccountbyuseridCall = new GetAccountByUserIdCall();
			getaccountbyuseridBean.setUserId(uid);
			getaccountbyuseridBean.setAccountType(1);
			getaccountbyuseridBean.setAppId("AutoTest");
			getaccountbyuseridCall.setData(false, getaccountbyuseridBean);
			getaccountbyuseridCall.callService();
			aid = getaccountbyuseridCall.getAccountId();
			Logger.comment("获取一个AccountId:" + aid);
			// 如果没获取到会出错 空指针异常，走自动创建流程
		} catch (Exception e) {
			try {
				Logger.comment("没有获取到AccountId,自动创建一个新的");
				CreateAccountBean createaccountBean = new CreateAccountBean();
				CreateAccountCall createaccountCall = new CreateAccountCall();
				createaccountBean.setAccountType(1);
				createaccountBean.setAppId("AutoTest");
				createaccountBean.setCurrencyType(1);
				createaccountBean.setMemo("自动化测试专用");
				createaccountBean.setUserId(uid);
				createaccountCall.setData(false, createaccountBean);
				createaccountCall.callService();
				Logger.comment("没有获取到AccountId，创建一个:"
						+ createaccountCall.getReturnData());
				aid = createaccountCall.getAccountId();
			} catch (Exception e1) {
				// 创建也报错时,打印错误,抛出异常
				e1.printStackTrace();
				throw e1;
			}
		}
		return aid;
	}
	/**
	 * 根据uid和accounttype 产生accountid
	 * @param uid
	 * @param atype
	 * @return
	 * @throws Exception
	 */
//	public static String getAccountId(int uid,int atype) throws Exception {
//		String aid = null;
//		try {
//			Logger.comment("获取Uid" + uid + "对应的AccountId");
//			// 查询Uid对应的Accountid
//			GetAccountByUserIdBean getaccountbyuseridBean = new GetAccountByUserIdBean();
//			GetAccountByUserIdCall getaccountbyuseridCall = new GetAccountByUserIdCall();
//			getaccountbyuseridBean.setUserId(uid);
//			getaccountbyuseridBean.setAccountType(atype);
//			getaccountbyuseridBean.setAppId("AutoTest");
//			getaccountbyuseridCall.setData(false, getaccountbyuseridBean);
//			getaccountbyuseridCall.callService();
//			aid = getaccountbyuseridCall.getAccountId();
//			Logger.comment("获取一个AccountId:" + aid);
//			// 如果没获取到会出错 空指针异常，走自动创建流程
//		} catch (Exception e) {
//			try {
//				Logger.comment("没有获取到AccountId,自动创建一个新的");
//				CreateAccountBean createaccountBean = new CreateAccountBean();
//				CreateAccountCall createaccountCall = new CreateAccountCall();
//				createaccountBean.setAccountType(atype);
//				createaccountBean.setAppId("AutoTest");
//				createaccountBean.setCurrencyType(1);
//				createaccountBean.setMemo("自动化测试专用");
//				createaccountBean.setUserId(uid);
//				createaccountCall.setData(false, createaccountBean);
//				createaccountCall.callService();
//				Logger.comment("没有获取到AccountId，创建一个:"
//						+ createaccountCall.getReturnData());
//				aid = createaccountCall.getAccountId();
//			} catch (Exception e1) {
//				// 创建也报错时,打印错误,抛出异常
//				e1.printStackTrace();
//				throw e1;
//			}
//		}
//		return aid;
//	}
	/**
	 * @param accountingBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void Accounting(AccountingBean accountingBean)
			throws ClientProtocolException, IOException {
		AccountingCall accountingCall = new AccountingCall();
		accountingCall.setData(false, accountingBean);
		accountingCall.callService();
	}
	/**
	 * 获取AccountId，如果没创建一个
	 * 
	 * @param uid UserId 
	 * @param accountType 账户类型
	 * @return accountId
	 * @throws Exception
	 */
	public static String getAccountId(Integer uid, int accountType)
			throws Exception {
		String aid = null;
		try {
			Logger.comment("获取Uid" + uid + "对应的AccountId");
			// 查询Uid对应的Accountid
			GetAccountByUserIdBean getaccountbyuseridBean = new GetAccountByUserIdBean();
			GetAccountByUserIdCall getaccountbyuseridCall = new GetAccountByUserIdCall();
			getaccountbyuseridBean.setUserId(uid);
			getaccountbyuseridBean.setAccountType(accountType);
			getaccountbyuseridBean.setAppId("AutoTest");
			getaccountbyuseridCall.setData(false, getaccountbyuseridBean);
			getaccountbyuseridCall.callService();
			aid = getaccountbyuseridCall.getAccountId();
			Logger.comment("获取一个AccountId:" + aid);

			// 如果没获取到会出错 空指针异常，走自动创建流程
		} catch (Exception e) {
			try {
				Logger.comment("没有获取到AccountId,自动创建一个新的");
				CreateAccountBean createaccountBean = new CreateAccountBean();
				CreateAccountCall createaccountCall = new CreateAccountCall();
				createaccountBean.setAccountType(accountType);
				createaccountBean.setAppId("AutoTest");
				createaccountBean.setCurrencyType(1);
				createaccountBean.setMemo("自动化测试专用");
				createaccountBean.setUserId(uid);
				createaccountCall.setData(false, createaccountBean);
				createaccountCall.callService();
				Logger.comment("没有获取到AccountId，创建一个:"
						+ createaccountCall.getReturnData());
				aid = createaccountCall.getAccountId();
			} catch (Exception e1) {
				// 创建也报错时,打印错误,抛出异常
				e1.printStackTrace();
				throw e1;
			}
		}
		return aid;
	}

	/**
	 * 获取没有AccountId的uid，尝试5次<br>
	 * 如果一次都没获取到 返回0
	 * 
	 * @return newUid
	 * @throws Exception
	 */
	public static int getNewUid() throws Exception {
		int nuid = 0;
		try {
			for (int i = 0; i < 5; i++) {
				nuid = 99000000 + Integer.valueOf(TestDataManager
						.getRandomNum(6));
				List<Account> list = new AccountWapper()
						.selectAccountInfoByUserId(nuid);
				Logger.debug("Account info:" + list);
				if (list.size() <= 0) {
					return nuid;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Logger.comment("获取一个AccountInfo表中没有的新UserId:" + nuid);
		return 0;
	}

	/**
	 * 锁定accountId
	 * 
	 * @param accountId
	 */
	public static void lockAccountId(String accountId) {
		try {
			Logger.comment("锁定账户:" + accountId);
			SetAccountLockStatusBean setaccountlockstatusBean = new SetAccountLockStatusBean();
			SetAccountLockStatusCall setaccountlockstatusCall = new SetAccountLockStatusCall();
			setaccountlockstatusBean.setAccountId(accountId);
			setaccountlockstatusBean.setStatus(1);
			setaccountlockstatusBean.setAppId("AutoTest");
			setaccountlockstatusBean.setMemo("自动化测试Lock");
			setaccountlockstatusCall.setData(false, setaccountlockstatusBean);
			setaccountlockstatusCall.callService();

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解锁accountId
	 * 
	 * @param accountId
	 */
	public static void unlockAccountId(String accountId) {
		try {
			Logger.comment("解锁账户:" + accountId);
			SetAccountLockStatusBean setaccountlockstatusBean = new SetAccountLockStatusBean();
			SetAccountLockStatusCall setaccountlockstatusCall = new SetAccountLockStatusCall();
			setaccountlockstatusBean.setAccountId(accountId);
			setaccountlockstatusBean.setStatus(0);
			setaccountlockstatusBean.setAppId("AutoTest");
			setaccountlockstatusBean.setMemo("自动化测试UnLock");
			setaccountlockstatusCall.setData(false, setaccountlockstatusBean);
			setaccountlockstatusCall.callService();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取Fundin 信息
	 * 
	 * @param accountId 账户ID
	 * @param amount 发生金额
	 * @param bizCode 业务编码
	 * @param originalNo 原始单据号
	 * @return AccountingItemBean
	 * @throws InterruptedException
	 */
	public static AccountingItemBean getFundinBean(String accountId,
			BigDecimal amount, int bizCode, String originalNo)
			throws InterruptedException {
		if (accountId != null && originalNo != null) {
			Logger.comment(accountId + "账号余额增加" + amount + ",原始单据:"
					+ originalNo);
		}
		AccountingItemBean aib = new AccountingItemBean();
		aib.setAccountId(accountId);
		aib.setAccountingDate(YMTDateUtil
				.getSysTime("yyyy-MM-dd HH:mm:ss.SSSSSS"));
		aib.setAccountOperateType(AccountOperateTypeEnum.Fundin.getValue());
		aib.setAmount(amount);
		aib.setBizCode(bizCode);
		aib.setBizNo(TestDataManager.getRandomUUID().toString());
		aib.setCurrencyType(1);
		aib.setMemo("自动化测试:" + AccountOperateTypeEnum.Fundin);
		aib.setOperator("AutoTestOperator");
		aib.setOriginalNo(originalNo);
		return aib;

	}

	/**
	 * 获取Fundout 信息
	 * 
	 * @param accountId 账户ID
	 * @param amount 发生金额
	 * @param bizCode 业务编码
	 * @param originalNo 原始单据号
	 * @return AccountingItemBean
	 * @throws InterruptedException
	 */
	public static AccountingItemBean getFundoutBean(String accountId,
			BigDecimal amount, int bizCode, String originalNo)
			throws InterruptedException {
		Logger.comment(accountId + "账号余额减少" + amount + ",原始单据:" + originalNo);
		AccountingItemBean aib = new AccountingItemBean();
		aib.setAccountId(accountId);
		aib.setAccountingDate(YMTDateUtil
				.getSysTime("yyyy-MM-dd HH:mm:ss.SSSSSS"));
		aib.setAccountOperateType(AccountOperateTypeEnum.Fundout.getValue());
		aib.setAmount(amount);
		aib.setBizCode(bizCode);
		aib.setBizNo(TestDataManager.getRandomUUID().toString());
		aib.setCurrencyType(1);
		aib.setMemo("自动化测试:" + AccountOperateTypeEnum.Fundout);
		aib.setOperator("AutoTestOperator");
		aib.setOriginalNo(originalNo);
		return aib;

	}

	/**
	 * 获取Freeze 信息
	 * 
	 * @param accountId 账户ID
	 * @param amount 发生金额
	 * @param bizCode 业务编码
	 * @param originalNo 原始单据号
	 * @return AccountingItemBean
	 * @throws InterruptedException
	 */
	public static AccountingItemBean getFreezeBean(String accountId,
			BigDecimal amount, int bizCode, String originalNo)
			throws InterruptedException {
		Logger.comment(accountId + "账号冻结增加" + amount + ",原始单据:" + originalNo);
		AccountingItemBean aib = new AccountingItemBean();
		aib.setAccountId(accountId);
		aib.setAccountingDate(YMTDateUtil
				.getSysTime("yyyy-MM-dd HH:mm:ss.SSSSSS"));
		aib.setAccountOperateType(AccountOperateTypeEnum.Freeze.getValue());
		aib.setAmount(amount);
		aib.setBizCode(bizCode);
		aib.setBizNo(TestDataManager.getRandomUUID().toString());
		aib.setCurrencyType(1);
		aib.setMemo("自动化测试:" + AccountOperateTypeEnum.Freeze);
		aib.setOperator("AutoTestOperator");
		aib.setOriginalNo(originalNo);
		return aib;

	}

	/**
	 * 获取Freeze 信息
	 * 
	 * @param accountId 账户ID
	 * @param amount 发生金额
	 * @param bizCode 业务编码
	 * @param originalNo 原始单据号
	 * @return AccountingItemBean
	 * @throws InterruptedException
	 */
	public static AccountingItemBean getUnfreezeBean(String accountId,
			BigDecimal amount, int bizCode, String originalNo)
			throws InterruptedException {
		Logger.comment(accountId + "账号冻结减少" + amount + ",原始单据:" + originalNo);
		AccountingItemBean aib = new AccountingItemBean();
		aib.setAccountId(accountId);
		aib.setAccountingDate(YMTDateUtil
				.getSysTime("yyyy-MM-dd HH:mm:ss.SSSSSS"));
		aib.setAccountOperateType(AccountOperateTypeEnum.Unfreeze.getValue());
		aib.setAmount(amount);
		aib.setBizCode(bizCode);
		aib.setBizNo(TestDataManager.getRandomUUID().toString());
		aib.setCurrencyType(1);
		aib.setMemo("自动化测试:" + AccountOperateTypeEnum.Unfreeze);
		aib.setOperator("AutoTestOperator");
		aib.setOriginalNo(originalNo);
		return aib;
	}

	/**
	 * 初始化账户，解锁后设置余额
	 * 
	 * @param accountid1 账户id
	 * @param newAmount 余额
	 * @param newFreeze 冻结金额
	 * @return 
	 */
	public static AccountingBean resetAccount(String accountid1,
			BigDecimal newAmount, BigDecimal newFreeze) {
		try {
			unlockAccountId(accountid1);
			Logger.comment("初始化账户：" + accountid1 + " 余额:" + newAmount + "冻结:"
					+ newFreeze);
			Account m = new AccountWapper().selectAccountInfo(accountid1)
					.get(0);
			BigDecimal amount = m.getAvailAmount();
			BigDecimal freezeamount = m.getFreezeAmount();
			AccountingBean accountingBean = new AccountingBean();
			AccountingCall accountingCall = new AccountingCall();
			accountingBean.setAppId("AutoTest");
			List<AccountingItemBean> AccountingItems = new ArrayList<AccountingItemBean>();
			String oNo = TestDataManager.getRandomUUID().toString();
			if (amount.floatValue() > 0) {
				AccountingItems.add(AccountCallServiceV2.getFundoutBean(
						accountid1, amount, 100001, oNo));
			}
			if (freezeamount.floatValue() > 0) {
				AccountingItems.add(AccountCallServiceV2.getUnfreezeBean(
						accountid1, freezeamount, 100001, oNo));
			}
			if (newAmount.floatValue() > 0) {
				AccountingItems.add(AccountCallServiceV2.getFundinBean(
						accountid1, newAmount, 100001, oNo));
			}
			if (newFreeze.floatValue() > 0) {
				AccountingItems.add(AccountCallServiceV2.getFreezeBean(
						accountid1, newFreeze, 100001, oNo));
			}
			accountingBean.setAccountingItems(AccountingItems);
			accountingCall.setData(false, accountingBean);
			accountingCall.callService();
			return accountingBean;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 根据userId，账户类型	查询用户余额
	 * @param userId
	 * @param accountType
	 * @return	availAmount
	 */
	public static BigDecimal getAccountBalance(int userId, int accountType) {

		BigDecimal availAmount = BigDecimal.ZERO;

		GetAccountBalanceBean getaccountbalanceBean = new GetAccountBalanceBean();
		GetAccountBalanceCall getaccountbalanceCall = new GetAccountBalanceCall();

		getaccountbalanceBean.setUserId(userId);
		getaccountbalanceBean.setAccountType(accountType);
		getaccountbalanceBean.setAppId("AutoTest");
		getaccountbalanceCall.setData(false, getaccountbalanceBean);

		try {

			getaccountbalanceCall.callService();

			availAmount = new BigDecimal(getaccountbalanceCall.getAvailAmount());

		} catch (Exception e) {
			Logger.info(String.format("##########查询用户:%s,账户类型:%s 账户余额失败",
					userId, accountType));
		}

		return availAmount;

	}
	/**
	 * 根据userId，查询用户资金流水 默认查询返回最近的4笔资金流水
	 * @param userId
	 * @return
	 */
	public static List<AccountEntryBean> getAccountentrylist(int userId) {

		GetAccountEntryListBean getaccountentrylistBean = new GetAccountEntryListBean();
		GetAccountEntryListCall getaccountentrylistCall = new GetAccountEntryListCall();

		int pageIndex = 1;
		int pageSize = 10;

		List<AccountEntryBean> accountEntryList = new ArrayList<AccountEntryBean>();

		try {

			String accountid = getAccountId(userId);

			getaccountentrylistBean.setAccountId(accountid);
			getaccountentrylistBean.setAppId("AutoTest");
			getaccountentrylistBean.setPageIndex(pageIndex);
			getaccountentrylistBean.setPageSize(pageSize);
			getaccountentrylistCall.setData(false, getaccountentrylistBean);

			getaccountentrylistCall.callService();

			accountEntryList = getaccountentrylistCall.getReturnAccountList();

		} catch (Exception e) {

			Logger.info(String.format("##########查询用户:%s 账户资金流水失败", userId));
		}

		return accountEntryList;

	}
	public static void main(String[] args) throws Exception {

		String accountid = AccountCallServiceV2.getAccountId(24761611);
		AccountCallServiceV2.resetAccount(accountid, new BigDecimal(99999999),
				new BigDecimal(0));

		//AccountCallServiceV2.getAccountentrylist(20238699);

	}
}
